package com.dongway.netcondition.service;

import java.util.HashMap;
import java.util.Map;
import java.util.Properties;
import java.util.Set;

import org.slf4j.Logger;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import org.springframework.core.io.support.EncodedResource;
import org.springframework.core.io.support.PropertiesLoaderUtils;
import org.springframework.stereotype.Service;

@Service
public class StateHelper {

    private static final Logger logger = org.slf4j.LoggerFactory.getLogger(StateHelper.class);
    private static final Map<Integer, String> stateCache = new HashMap<Integer, String>();

    static {
        try {
            Resource resource = new ClassPathResource("netCondition.properties");
            Properties prop =
                    PropertiesLoaderUtils.loadProperties(new EncodedResource(resource, "utf-8"));
            Set<Object> keys = prop.keySet();
            for (Object o : keys) {
                String key = o.toString();
                if (key.startsWith("state.")) {
                    stateCache.put(Integer.valueOf(key.substring(6)), prop.getProperty(key));
                }
            }
        } catch (Exception e) {
            logger.warn(e.getMessage(), e);
        }
    }


    public String getState(Integer i) {
        return stateCache.get(i);
    }
}
